package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.view.ContextThemeWrapper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.HotSendLinkItemAdapter;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class ShopSetImageViewActivity extends BaseActivity implements View.OnClickListener{

    private ImageView setviewback;
    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
    private ImageView img4;
    private ImageView img5;
    private ImageView img6;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private String shopId;
    private String xy;
    private String serviceid;
    private ImageView backmain;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_set_image_view);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        shopId = sharedPreferences.getString("shopid", "");
        Intent intent = getIntent();
        serviceid = intent.getStringExtra("serviceid");
        initSetViewView();
        getServiceView();
    }
        // 获取服务展位
    private void getServiceView() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.SHOPGETSERVICEVIEW);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("shopId",shopId);
       params.addParameter("params",object.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                JSONObject obj = JSON.parseObject(result);
                JSONArray data = obj.getJSONArray("data");
                if (data.size()!=0){
                    for (int i = 0; i < data.size(); i++) {
                        JSONObject obj1 = JSON.parseObject(String.valueOf(data.get(i)));
                        String serviceAvatar = obj1.getString("serviceAvatar");
                        String booth = obj1.getString("booth");
                        if (booth.equals("1")){
                            x.image().bind(img1,InternetAddressUtis.IMG_URL+serviceAvatar);
                        }else if (booth.equals("2")){
                            x.image().bind(img2,InternetAddressUtis.IMG_URL+serviceAvatar);
                        }else if (booth.equals("3")){
                            x.image().bind(img3,InternetAddressUtis.IMG_URL+serviceAvatar);
                        }else if (booth.equals("4")){
                            x.image().bind(img4,InternetAddressUtis.IMG_URL+serviceAvatar);
                        }else if (booth.equals("5")){
                            x.image().bind(img5,InternetAddressUtis.IMG_URL+serviceAvatar);
                        }else {
                            x.image().bind(img6,InternetAddressUtis.IMG_URL+serviceAvatar);
                        }
                    }
                }else {}

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(ShopSetImageViewActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initSetViewView() {
        setviewback = (ImageView) findViewById(R.id.setimageview_back);
        img1 = (ImageView) findViewById(R.id.setview_img1);
        img2 = (ImageView) findViewById(R.id.setview_img2);
        img3 = (ImageView) findViewById(R.id.setview_img3);
        img4 = (ImageView) findViewById(R.id.setview_img4);
        img5 = (ImageView) findViewById(R.id.setview_img5);
        img6 = (ImageView) findViewById(R.id.setview_img6);
        backmain = (ImageView) findViewById(R.id.shopset_backmain);

        backmain.setOnClickListener(this);

        setviewback.setOnClickListener(this);
        img1.setOnClickListener(this);
        img2.setOnClickListener(this);
        img3.setOnClickListener(this);
        img4.setOnClickListener(this);
        img5.setOnClickListener(this);
        img6.setOnClickListener(this);
    }

    /**
     *
     * 点击设置展位
     *
     * @param v
     *
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.shopset_backmain:
                startActivity(new Intent(ShopSetImageViewActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.setimageview_back:
                finish();
                break;
            case R.id.setview_img1:
                xy="1";
                toastDialog(xy,serviceid);
                break;
            case R.id.setview_img2:
                xy="2";
                toastDialog(xy,serviceid);

                break;
            case R.id.setview_img3:
                xy="3";
                toastDialog(xy,serviceid);

                break;
            case R.id.setview_img4:
                xy="4";
                toastDialog(xy,serviceid);

                break;
            case R.id.setview_img5:
                xy="5";
                toastDialog(xy,serviceid);

                break;
            case R.id.setview_img6:
                xy="6";
                toastDialog(xy,serviceid);

                break;
        }
    }
        // 设置弹窗
    private void toastDialog(final String s, final String xy) {
        AlertDialog.Builder builder = new AlertDialog.Builder(ShopSetImageViewActivity.this,R.style.NoBackGroundDialog);
        View view = LayoutInflater.from(ShopSetImageViewActivity.this).inflate(R.layout.cancel_servicedialog, null);
        Button cancelbt = (Button) view.findViewById(R.id.cancel_cancelbt);
        Button surebt = (Button) view.findViewById(R.id.cancel_surebt);
        TextView t2 = (TextView) view.findViewById(R.id.cancel_t2);
        builder.setView(view);
        t2.setText("是否将服务放置"+s+"展位？");
        final AlertDialog alertDialog = builder.create();
        cancelbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        surebt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                alertDialog.dismiss();
                setServiceView(xy,s);
            }
        });
        alertDialog.show();


    }


    // 设置展位
    private void setServiceView(String id,String index) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.SHOPSETVIEWSERVICE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("serviceId",id);
        object.put("booth",index);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Toast.makeText(ShopSetImageViewActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                img1.setImageResource(R.mipmap.img1);
                img2.setImageResource(R.mipmap.img2);
                img3.setImageResource(R.mipmap.img3);
                img4.setImageResource(R.mipmap.img4);
                img5.setImageResource(R.mipmap.img5);
                img6.setImageResource(R.mipmap.img6);
                getServiceView();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(ShopSetImageViewActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }




}
